package com.baizhi.lql.controller;

import com.baizhi.lql.dto.Dto;
import com.baizhi.lql.entity.Video;
import com.baizhi.lql.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("video")
@Controller
@CrossOrigin
public class VideoController {
    @Autowired
    private VideoService videoService;

    @RequestMapping("queryAllPage")
    @ResponseBody
    public Map<String,Object> queryAllPage(@RequestBody Dto dto){
        return videoService.queryAllPage(dto.getPage(), dto.getPageSize());
    }
    @RequestMapping("uploadHeadImg")
    @ResponseBody
    public Map<String,String> uploadHeadImg(MultipartFile videoFile,String oldVideo){
        Map<String,String> map = new HashMap<>();
        try{
            String path = videoService.uploadHeadImg(videoFile,oldVideo);
            map.put("fileName",path);
            map.put("message","上传成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","上传失败");
        }
        return map;
    }
    @RequestMapping("add")
    @ResponseBody
    public Map<String,String> add(@RequestBody Video video){
        Map<String,String> map = new HashMap<>();
        try{
            videoService.add(video);
            map.put("message","添加成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","添加失败");
        }
        return map;
    }
    @RequestMapping("delete")
    @ResponseBody
    public Map<String,String> delete(@RequestBody Dto dto){
        Map<String,String> map = new HashMap<>();
        try{
            videoService.remove(dto.getId());
            map.put("message","删除成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","删除失败");
        }
        return map;
    }
    @RequestMapping("update")
    @ResponseBody
    public Map<String,String> update(@RequestBody Video video){
        Map<String,String> map = new HashMap<>();
        try{
            videoService.modify(video);
            map.put("message","修改成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","修改失败");
        }
        return map;
    }
    @RequestMapping("queryById")
    @ResponseBody
    public Video queryById(int id){
        return videoService.queryOne(id);
    }
}
